
import GlobalEvent from "../../../cfw/event/GlobalEvent";
import { EventName } from "../../../public/Config";
import CookeryRoomStep from "./CookeryRoomStep";
import CollisionHelper from "../../../cfw/collide/CollisionHelper";
import BottleItemView from "./BottleItemView";
import DrawV2 from "../../../engine/comps/DrawV2";
import CookeryController from "../CookeryController";
import SDKManager from "../../../sdk/SDKManager";
import { ResultState } from "../../../sdk/SDKConfig";
import CookeryManager from "../CookeryManager";
import { ItemState } from "../../../cfw/tools/Define";
const { ccclass, property } = cc._decorator;

@ccclass
export default class CookeryRoomStep6 extends CookeryRoomStep {

    @property([BottleItemView])
    bottles: BottleItemView[] = [];


    @property(DrawV2)
    draw: DrawV2 = null;

    protected curBottle: BottleItemView = null;

    protected initPos: cc.Vec3 = cc.v3()

    protected controller: CookeryController;

    protected model: CookeryManager;

    protected colors:string[] = ['#FB61AA','#F5C842']

    onButtonNextClick() {
        if (SDKManager.getChannel().cantUseImageDataURL()) {
            this.controller.setTextureInfo(this.draw.getTextureInfo())
        } else {
            this.controller.setDataUrl(this.draw.getDataUrl())
        }


        this.finish()
    }

    onButtonClearClick() {
        this.draw.clear()
    }

    start() {
        this.controller = CookeryController.instance();
        let list = this.model.getBottleList()
        for (let index = 0; index < this.bottles.length; index++) {
            const element = this.bottles[index];
            if (list[index]) {
                element.setModel(list[index])
            }
        }
        this.node.active = true;
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this)
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this)
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchCancel, this)
    }

    touchCancel() {

        if (this.curBottle != null) {
            this.curBottle.node.position = this.initPos;
            this.curBottle = null;
        }
    }

    touchStart(e: cc.Touch) {
        let pos = e.getLocation()
        pos = this.node.convertToNodeSpaceAR(pos)
        for (let index = 0; index < this.bottles.length; index++) {
            const element = this.bottles[index];
            // let size = element.getContentSize()
            if (CollisionHelper.isPosInRotationRect(pos, element.node)) {

                if (element.needBuy()) {
                    this.controller.openItem(element.getModel())
                } else {
                    this.curBottle = element;
                    this.draw.setColor(this.curBottle.point.color)
                    this.initPos = this.curBottle.node.position;
                    cc.log(' 碰到 第 ', index)
                }


                break;
            }
        }
    }

    touchMove(e: cc.Touch) {
        if (this.curBottle) {
            let pos = e.getLocation()
            let wpos = this.node.convertToNodeSpaceAR(pos)
            // cc.log(' wpos  ', wpos.x, wpos.y)
            this.curBottle.node.x = wpos.x
            this.curBottle.node.y = wpos.y;
            let b = this.curBottle.point.getPosition();
            b = this.curBottle.node.convertToWorldSpaceAR(b)
            this.draw.paintStart(this.draw.node.convertToNodeSpaceAR(b))
        }
    }
    finish() {
        cc.log(' finish == ')
        GlobalEvent.instance().emit(EventName.UPDATE_COOKIE_STEP, 6)
    }


}
